<template>
  <div class="app-container">
    <div
      v-show="
        !detailInfo.visible &&
        !assetDetailInfo.visible &&
        !assetCardModifyInfo.visible
      "
    >
      <!-- 条件栏 -->
      <div class="filter-wrapper">
        <NewPageFilter
          v-model:query="filterInfo.query.queryMap"
          :query-more-json="true"
          :filter-list="filterInfo.searchList"
          :filter-btn="filterInfo.searchBtn"
          :clear-type="clearType"
          :slip-select-request="slipSelectRequest"
        >
        </NewPageFilter>
      </div>
      <div class="filter-wrapper flex-end">
        <div class="display-flex">
          <el-button @click="preferenceInfo.visible = true">偏好设置</el-button>
        </div>
      </div>
      <!-- 表格 -->
      <table-container
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="tableInfo.api"
        :query="filterInfo.query"
        :field-list="tableInfo.fieldList"
        :handle="tableInfo.handle"
        :row-class-name="tableRowClassName"
        @sortChange="sortChange"
        @handleEvent="handleEvent"
      >
        <template v-slot:col-orderNo="scope">
          <span class="slip-edit" @click="showDetail(scope.row)">{{
            scope.row.orderNo
          }}</span>
        </template>
        <template v-slot:col-assetName="scope">
          <span>{{ scope.row.assetName }}</span>
        </template>
      </table-container>
    </div>
    <set-preference
      v-if="preferenceInfo.visible"
      v-model:visible="preferenceInfo.visible"
      :search-list="filterInfo.searchList"
      :field-list="tableInfo.fieldList"
      :customize-user-v-o="filterInfo.query.customizeUserVO"
      :group-level-show="false"
      @callback="setPreferenceCallback"
    />
    <advanced-search
      v-if="advancedSearchInfo.visible"
      v-model:visible="advancedSearchInfo.visible"
      v-model:form-data="advancedSearchInfo.data"
      :field-list="tableInfo.fieldList"
      @callback="advancedSearchCallback"
    />
    <order-detail
      v-if="detailInfo.visible"
      v-model:visible="detailInfo.visible"
      :table-row-id="detailInfo.id"
      @returnBack="getList"
    />
    <asset-card-detail
      v-if="assetDetailInfo.visible"
      :id="assetDetailInfo.id"
      v-model:visible="assetDetailInfo.visible"
      :title="assetDetailInfo.title"
    />
    <AssetCardModify
      v-if="assetCardModifyInfo.visible"
      v-model:visible="assetCardModifyInfo.visible"
      :title="assetCardModifyInfo.title"
      :type="assetCardModifyInfo.type"
      :current-accounting-interval="
        assetCardModifyInfo.currentAccountingInterval
      "
      :inventoryLocation="assetCardModifyInfo.inventoryLocation"
      :buildCardData="assetCardModifyInfo.buildCardData"
      :id="assetCardModifyInfo.id"
      :fillData="assetCardModifyInfo.fillData"
      @returnModify="getList"
    />
  </div>
</template>

<script setup name="inventorySurplusAudit">
import TableContainer from '@/components/TableContainer/index'
import AssetCardModify from '@/views/asset/assetManage/assetCard/modify'
import {
  getInventorySurplusCustomizeListPage,
  ratifyInventorySurplus,
} from '@/api/asset.js'
import { getCurrentFiscalPeriod } from '@/api/core.js'
import { getCustomizeSetting, updateUserCustomize } from '@/api/customize'
import OrderDetail from '../assetInventoryTask/details'
import AssetCardDetail from '@/views/asset/assetManage/assetCard/details'
const { proxy } = getCurrentInstance()
// 过滤相关配置

const filterInfo = ref({
  sortArr: [],
  copyQueryMap: {},
  query: {
    sortString: '',
    customizeUserVO: {},
    queryMap: {},
  },
  searchList: [],
  searchBtn: [
    {
      type: 'button',
      label: '搜索',
      event: search,
    },
    {
      type: 'button',
      label: '高级查询',
      event: advancedSearchShow,
    },
    {
      type: 'button',
      label: '重置',
      event: clearSearch,
    },
  ],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: undefined,
  selections: [],
  fieldList: [],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '100',
    btList: [
      {
        label: '生成资产卡片',
        event: audit,
        show: proxy.$permission('inventorySurplusAudit:audit'),
        ifRender: function (row) {
          return row.status !== 3 && row.isCreateAssetCard == 0
        },
      },
    ],
  },
})
const preferenceInfo = ref({
  visible: false,
})
const advancedSearchInfo = ref({
  visible: false,
  data: {},
})
const assetDetailInfo = ref({
  visible: false,
  id: '',
  title: '资产详情',
})
const detailInfo = ref({
  visible: false,
  id: '',
})
const assetCardModifyInfo = ref({
  visible: false,
  title: '',
  id: '',
  currentAccountingInterval: '',
  type: '',
  inventoryLocation: '',
  buildCardData: [],
  fillData: {},
})
const clearType = ref(true)
const slipSelectRequest = ref(true)
const canAudit = computed(() => {
  return (
    tableInfo.value.selections.length &&
    tableInfo.value.selections.filter((item) => item.status == 1).length ===
      tableInfo.value.selections.length
  )
})

const obj = ref({
  statusValue: {
    label: '状态',
    value: 'statusValue',
    asValue: 'status',
    componentType: 'select-model',
    code: 'asset_inventory_surplus_status',
    show: false,
    hidden: true,
  },
  orderNo: {
    label: '任务单号',
    value: 'orderNo',
    componentType: 'input',
    type: 'slot',
    show: false,
    hidden: true,
  },
})
tableInfo.value.fieldList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...proxy.$fn.deepClone(obj.value),
})
filterInfo.value.searchList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...proxy.$fn.deepClone(obj.value),
})
getCustomize()
function audit(row = undefined) {
  getCurrentFiscalPeriod(2).then((resp) => {
    if (resp && resp.success && resp.data) {
      assetCardModifyInfo.value.currentAccountingInterval = resp.data.startDate
    } else {
      proxy.$message({
        type: 'error',
        duration: 3500,
        message: '未开启会计期间，请先开启会计期间!',
        showClose: true,
      })
    }
    assetCardModifyInfo.value.type = 'createSurplus'
    assetCardModifyInfo.value.visible = true
    assetCardModifyInfo.value.title = '新增盘盈资产卡片'
    assetCardModifyInfo.value.inventoryLocation = row.inventoryLocation
    let assetCapitalSourceBusinessVOList = row.assetCapitalSourceBusinessVOList
    if (assetCapitalSourceBusinessVOList.length) {
      assetCapitalSourceBusinessVOList.forEach((item) => {
        item.id = null
      })
    } else {
      assetCapitalSourceBusinessVOList = [{ code: '', amount: 0 }]
    }
    assetCardModifyInfo.value.fillData = {
      assetCapitalSourceVOList: assetCapitalSourceBusinessVOList,
      assetCapitalSourceValue: row.assetCapitalSourceValue || '',
      assetName: row.assetName || '',
      assetCode: row.assetCode || '',
      standard: row.standard || '',
      brandName: row.brandName || '',
      mark: row.mark || '',
      deviceTypeValue: row.deviceTypeValue || '',
      deviceType: row.deviceType || '',
      temporaryPurchValue: row.temporaryPurchValue || '',
      temporaryPurch: row.temporaryPurch === -2 ? '' : row.temporaryPurch || '',
      supplierName: row.supplierName || '',
      supplierId: row.supplierId || '',
      originPlace: row.originPlace || '',
      useStatus: row.useStatus === -2 ? '' : row.status || '',
      useStatusValue: row.useStatusValue || '',
      accrualMark: row.accrualMark === -2 ? '' : row.accrualMark || '',
      accrualMarkValue: row.accrualMarkValue || '',
      useDeptName: row.useDeptName || '',
      useDeptId: row.useDeptId || '',
      depotName: row.depotName || '',
      depotId: row.depotId || '',
      manageDeptName: row.manageDeptName || '',
      manageDeptId: row.manageDeptId || '',
      increaseType: row.increaseType === -2 ? '' : row.increaseType || '',
      increaseTypeValue: row.increaseTypeValue || '',
      unitId: row.unitId === -2 ? '' : row.unitId || '',
      unitName: row.unitName || '',
      qty: row.qty || 0,
      primitiveOriginalAmount: row.primitiveOriginalAmount || 0,
      classifyTypeValue: row.classifyTypeValue || '',
      classifyType: row.classifyType || '',
      assetMark: row.assetMark || '',
      useUserName: row.useUserName || '',
      useUserId: row.useUserId || '',
      location: row.location || '',
      orderSourceType: 52,
      orderSourceId: row.id,
      orderSourceNo: row.orderNo,
    }
  })
  console.log(11222)
}
async function setPreferenceCallback(obj) {
  updateUserCustomize(obj.customizeUserVO).then((res) => {
    if (res && res.success) {
      getCustomize(!slipSelectRequest.value)
    }
  })
}
function advancedSearchShow() {
  advancedSearchInfo.value.visible = true
}
async function advancedSearchCallback(obj) {
  filterInfo.value.query.queryMap = {}
  filterInfo.value.query.queryDtoList = [...obj.queryDtoList]
  advancedSearchInfo.value.data = obj.data
  await getList()
  clearType.value = !clearType.value
  filterInfo.value.query.queryMap = proxy.$fn.deepClone(
    filterInfo.value.copyQueryMap
  )
}
// 获取列表
async function getList() {
  tableInfo.value.refresh = Math.random()
}
// 搜索
function search() {
  // 重置分页

  advancedSearchInfo.value.data = {}
  filterInfo.value.query.queryDtoList = []
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}

//展示明细
function showDetail(row) {
  sessionStorage.setItem('storeRowId', row.id)
  detailInfo.value.id = row.taskOrderId
  detailInfo.value.visible = true
}
function showAssetDetail(row) {
  sessionStorage.setItem('storeRowId', row.id)
  assetDetailInfo.value.id = row.assetId
  assetDetailInfo.value.visible = true
}
function tableRowClassName({ row }) {
  let rowId = sessionStorage.getItem('storeRowId')
  if (rowId && row.id === Number(rowId)) {
    return 'check-row'
  }
  return ''
}
// 触发事件
function handleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      tableInfo.value.selections = [...data]
      break
  }
}
function clearSearch() {
  clearType.value = !clearType.value
  advancedSearchInfo.value.data = {}
  filterInfo.value.query.queryMap = proxy.$fn.deepClone(
    filterInfo.value.copyQueryMap
  )
  search()
}
function getCustomize(_slipSelectRequest = true) {
  let param = {
    tableKey:
      'assetAssetManageInventorySurplusList-com.slip.spd.entity.vo.asset.AssetInventorySurplusVO',
  }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      filterInfo.value.query.customizeUserVO = data
      tableInfo.value.fieldList = proxy.$fn.setFieldList(
        tableInfo.value.fieldList,
        data.detail
      )
      let searchInfo = proxy.$fn.setSearchList(
        filterInfo.value.searchList,
        data.detail
      )
      filterInfo.value.query.queryMap = {
        ...searchInfo.queryMap,
        ...filterInfo.value.query.queryMap,
      }
      filterInfo.value.searchList = searchInfo.searchList
      filterInfo.value.searchList.forEach((item) => {
        item.event = search
      })
      filterInfo.value.copyQueryMap = proxy.$fn.deepClone(
        filterInfo.value.query.queryMap
      )
      slipSelectRequest.value = _slipSelectRequest
      tableInfo.value.api = getInventorySurplusCustomizeListPage
      getList()
    }
  })
}
function sortChange(column) {
  if (filterInfo.value.query.customizeUserVO.head.sortType === 0) {
    let arr = proxy.$fn.sortChange(column, filterInfo.value.sortArr)
    filterInfo.value.sortArr = arr
    filterInfo.value.query.sortString = arr.length ? arr.toString() : ''
    getList()
  }
}
</script>

<style scoped></style>
